package com.beamfield.controller.manage.operator;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.dictionary.code;
import com.beamfield.model.NormalOperator;
import com.beamfield.model.SystemDictionaryData;
import com.beamfield.modelVo.SystemDictionaryDataVo;
import com.beamfield.service.manage.sys.DictonaryService;
import com.beamfield.service.manage.sys.FileService;
import com.beamfield.service.manage.operator.ManagerInfoService;
/**
 * 普通人员
 * @author zhaohh
 *
 */
@Controller
@RequestMapping("/cms/operator/managerInfo")
public class NormalOperatorController {
	
	@Value("${session.user}")
	private String user;
	
	@Value("${session.beam}")
	private String beam;

	@Resource
	ManagerInfoService managerInfoService;
	
	@Resource
	FileService fileService;
	
	@Resource
	DictonaryService dictonaryService;
	
	/**
	 * 人员管理页面
	 * @return
	 */
	@RequestMapping("/normalOperator")
	public ModelAndView normalOperator(){
		return new ModelAndView();
	}
	/**
	 * 获取管理人员列表
	 * @param offset
	 * @param limit
	 * @param ecode
	 * @param name
	 * @return
	 */
	@RequestMapping("/getNormalOperatorList")
	@ResponseBody
	public PageResult getNormalOperatorList(Integer offset, Integer limit,String ecode,String name,String presasg, String group, HttpSession session){
		Long beam_id = (Long) session.getAttribute(beam);
		return managerInfoService.getNormalOperatorList( offset,  limit, ecode, name,beam_id, presasg, group);
	}
	
	/**
	 * 编辑或新增页面
	 * @param
	 * @return
	 */
	@RequestMapping("/editNormalOperator")
	public ModelAndView editNormalOperator(){
		ModelAndView modelAndView = new ModelAndView();
		return modelAndView;
	}
	
	@RequestMapping("/getNormalOperatorEditInfo")
	@ResponseBody
	public ApiResult getNormalOperatorEditInfo(Long id,HttpSession session){
		ApiResult apiResult = new ApiResult();
		long beam_id = (long) session.getAttribute(beam);
		Map<String, Object> map = new HashMap<String, Object>();
		NormalOperator normalOperator = managerInfoService.getNormalOperatorById(id);
		if(normalOperator != null){
			normalOperator.setPic_url(CommonUtils.getFielUrl(fileService.getFileById(normalOperator.getAvatar())));
			
			if(normalOperator.getQrcode() != null){
				normalOperator.setQr_url(CommonUtils.getQrUrl(fileService.getFileById(normalOperator.getQrcode())));
			}

		}
//		List<SystemDictionaryDataVo> department = dictonaryService.getDicByCode(code.department,beam_id);
		List<SystemDictionaryDataVo> health = dictonaryService.getDicByCode(code.health,beam_id);
		List<SystemDictionaryDataVo> sex = dictonaryService.getDicByCode(code.sex,beam_id);
		List<SystemDictionaryDataVo> group = dictonaryService.getDicByCode(code.group,beam_id);
//		map.put("department", department);
		map.put("health", health);
		map.put("sex", sex);
		map.put("group", group);
		map.put("normalOperator", normalOperator);
		apiResult.setResult(map);
		return apiResult;
	}
	
	/**
	 * 详情页面
	 * @param
	 * @return
	 */
	@RequestMapping("/normalOperatorDetail")
	public ModelAndView normalOperatorDetail(){
		ModelAndView modelAndView = new ModelAndView();
		return modelAndView;
	}
	
	/**
	 * 获取详情数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/getNormalOperatorDetailInfo")
	@ResponseBody
	public ApiResult getNormalOperatorDetailInfo(Long id,HttpSession session){
		ApiResult apiResult = new ApiResult();
		long beam_id = (long) session.getAttribute(beam);
		Map<String, Object> map = new HashMap<String, Object>();
		NormalOperator normalOperator = managerInfoService.getNormalOperatorById(id);
		if(normalOperator != null){
			if(normalOperator.getAvatar() != null){
				normalOperator.setPic_url(CommonUtils.getFielUrl(fileService.getFileById(normalOperator.getAvatar())));
			}

			SystemDictionaryData sex = managerInfoService.selectByCode(normalOperator.getGender(),beam_id);
			
			if(sex != null){
				normalOperator.setGender(sex.getName());
			}
			
			SystemDictionaryData workdept = managerInfoService.selectByCode(normalOperator.getWorkdept(),beam_id);
			
			if(workdept != null){
				normalOperator.setWorkdept(workdept.getName());
			}
			
			SystemDictionaryData group = managerInfoService.selectByCode(normalOperator.getGroup(),beam_id);
			
			if(group != null){
				normalOperator.setGroup(group.getName());
			}
			
			if(normalOperator.getQrcode() != null){
				normalOperator.setQr_url(CommonUtils.getQrUrl(fileService.getFileById(normalOperator.getQrcode())));
			}
			SystemDictionaryData relation = managerInfoService.selectByCode(normalOperator.getRelation(),beam_id);

			if(relation != null){
				normalOperator.setRelation(relation.getName());
			}
			SystemDictionaryData health = managerInfoService.selectByCode(normalOperator.getHealth_condition(),beam_id);

			if(health != null){
				normalOperator.setHealth_condition(health.getName());
			}
		}
		List<SystemDictionaryDataVo> department = dictonaryService.getDicByCode(code.department,beam_id);
		List<SystemDictionaryDataVo> education = dictonaryService.getDicByCode(code.education,beam_id);
		List<SystemDictionaryDataVo> health = dictonaryService.getDicByCode(code.health,beam_id);
		List<SystemDictionaryDataVo> policital = dictonaryService.getDicByCode(code.policital,beam_id);
		List<SystemDictionaryDataVo> sex = dictonaryService.getDicByCode(code.sex,beam_id);
		List<SystemDictionaryDataVo> volk = dictonaryService.getDicByCode(code.volk,beam_id);
		map.put("department", department);
		map.put("education", education);
		map.put("health", health);
		map.put("policital", policital);
		map.put("sex", sex);
		map.put("volk", volk);
		map.put("normalOperator", normalOperator);
		apiResult.setResult(map);
		return apiResult;
	}
	
	/**
	 * 新增或编辑
	 * @return
	 */
	@RequestMapping("/doEditNormalOperator")
	@ResponseBody
	public ApiResult doEditNormalOperator(NormalOperator normalOperator,HttpSession session){
		Long user_id = (Long) session.getAttribute(user);
		Long beam_id = (Long) session.getAttribute(beam);
		
		normalOperator.setUpdater(user_id);
		normalOperator.setUpdatetime(new Date());
		normalOperator.setBeam_id(beam_id);
		
		Integer countIDCard = managerInfoService.checkCountManagerInfoIDCard(normalOperator.getIdentity(), normalOperator.getNormal_operator_id());
		Integer countIDCard2 = managerInfoService.checkCountNormalOperatorIDCard(normalOperator.getIdentity(), normalOperator.getNormal_operator_id());
		Integer countIDCard3 = managerInfoService.checkCountSpecialOperatorIDCard(normalOperator.getIdentity(), normalOperator.getNormal_operator_id());
		Integer namenum = managerInfoService.checkCountManagerInfoName(normalOperator.getName(), normalOperator.getNormal_operator_id(),beam_id);
		if (namenum > 0) {
			return new ApiResult("1", "姓名重复！");
		}
		if(countIDCard+countIDCard2+countIDCard3 > 0){
			return new ApiResult("1", "身份证号重复！");
		}
		
		try {
			//新增
			if(normalOperator.getNormal_operator_id() == null){
				Integer count = managerInfoService.getNormalOperatorCount();
				DecimalFormat format = new DecimalFormat("0000");
				String code = format.format(count+1);
				normalOperator.setEcode("03"+code);
				normalOperator.setOperator(user_id);
				normalOperator.setAddtime(new Date());
				normalOperator.setDelflag(0);
				normalOperator.setAdjust(0);
				managerInfoService.insertNormalOperator(normalOperator);
			}
			//编辑
			if(normalOperator.getNormal_operator_id() != null){
				managerInfoService.updateNormalOperator(normalOperator);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult("1", "服务器异常！");
		}
		
		return new ApiResult("2", "成功！");
	}
	
	/**
	 * 删除
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping("/delNormalOperator")
	@ResponseBody
	public ApiResult delNormalOperator(Long id,HttpSession session){
		NormalOperator normalOperator = managerInfoService.getNormalOperatorById(id);
		if(normalOperator == null){
			return new ApiResult("1","删除失败，查不到该数据!");
		}
		Long user_id = (Long) session.getAttribute(user);
		normalOperator.setUpdater(user_id);
		normalOperator.setUpdatetime(new Date());
		normalOperator.setDelflag(1);
		try {
			managerInfoService.updateNormalOperator(normalOperator);
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult("1","删除失败，服务器异常!");
		}
		return new ApiResult("2","成功!");
	}
	
	/**
	 * 特种作业的培训列表
	 * @param offset
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getDetailNormalOperatorList")
	@ResponseBody
	public PageResult getDetailNormalOperatorList(Integer offset, Integer limit,Long normal_operator_id,HttpSession session){
		return managerInfoService.getDetailNormalOperatorList( offset,  limit, normal_operator_id);
	}
	

	/**
	 * 导出数据
	 */
	@RequestMapping("/exportNormalOperatorDetail")
	@ResponseBody
	public String exportNormalOperatorDetail(HttpServletResponse response,String group,String ecode,String name, String presasg, HttpSession session) throws Exception{
		Long beam_id = (Long) session.getAttribute(beam);
		String apiResult = managerInfoService.exportNormalOperatorDetail(presasg, ecode,response,name,beam_id,group);
		return apiResult;
	}
	

	/**
	 * 考勤页面
	 * @return
	 */
	@RequestMapping("/normalOperatorAttend")
	public ModelAndView normalOperatorAttend(){
		ModelAndView modelAndView = new ModelAndView();
		return modelAndView;
	}
	
	/**
	 * 考勤列表
	 * @param offset
	 * @param limit
	 * @param name
	 * @return
	 */
	@RequestMapping("/getNormalOperatorAttendList")
	@ResponseBody
	public PageResult getNormalOperatorAttendList(Integer offset, Integer limit,String name,String time,String workdept,HttpSession session){
		Long beam_id = (Long) session.getAttribute(beam);
		return managerInfoService.getNormalOperatorAttendList( offset,  limit,name,time,workdept,beam_id);
	}
	
}
